<template>
	<view class="pageview" style="overflow-x: hidden;">
		<cl-navbar leftIconSize="40rpx" autoBack bgColor="transparent" :isbgColor="false">
		</cl-navbar>
		<view class="topImageView">
			<cl-stars-image :srckey="detailsInfo.cover_url"
				imageStyle="width: 750rpx;height: 580rpx;"></cl-stars-image>
		</view>
		<view class="contentView">
			<image src="@/static/nearby/huaban.png" mode="widthFix"
				style="width: 750rpx;height: 76rpx;position: absolute;left: 0;top: -94rpx;"></image>
			<view class="contentView__infoview">
				<view>
					<cl-stars-image :srckey="detailsInfo.cover_url"
						imageStyle="width: 150rpx;height: 150rpx;border-radius: 50%;"></cl-stars-image>
				</view>
				<view class="contentView__infoview__nameview">
					<text style="font-size: 34rpx;color: #FFFFFF;font-weight: 500;">{{detailsInfo.nick_name}}</text>
					<view class="contentView__infoview__nameview__idview">
						<text style="font-size: 28rpx;color: #a1a4b0;margin-right: 10rpx;">ID·{{maskString(detailsInfo.user_no)}}</text>
						<u-icon name="file-text" size="15px" color="#a1a4b0" @click="showID = true"></u-icon>
					</view>
				</view>
			</view>
			<view class="contentView__voice" v-if="detailsInfo.audio_info" @click="bofang">
				<image src="/static/nearby/bofang.png" mode="" v-show="!detailsInfo.audio_info.play"></image>
				<image src="/static/nearby/zanting.png" mode="" v-show="detailsInfo.audio_info.play"></image>
				<text>{{detailsInfo.audio_info.duration}}'</text>
			</view>
			<view class="contentView__tagsview">
				<text class="contentView__tagsview__item" v-for="(tag,tagidx) in detailsInfo.tags" :key="tag + tagidx">{{tag}}</text>
			</view>
			<view class="contentView__textview">
				{{detailsInfo.profile}}
			</view>
			<view class="contentView__wchatview" v-if="detailsInfo.contact_details">
				<text class="contentView__wchatview__title">联系方式</text>
				<view class="contentView__wchatview__infoview">
					<view class="contentView__wchatview__infoview__infodel">
						<image src="@/static/nearby/wchat.png" mode="" style=""></image>
						<text>{{maskString(detailsInfo.contact_details)}}</text>
					</view>
					<view class="contentView__wchatview__infoview__btn" @click="showWx = true">获取微信</view>
				</view>
			</view>
			<view class="contentView__photoAlbum">
				<text class="contentView__photoAlbum__title">TA相册</text>
				<view class="contentView__photoAlbum__phoneView">
					<u-album :urls="getAlbuma" rowCount="3" maxCount="100" :showMore="false" singleSize="200rpx"
						multipleSize="200rpx" space="18rpx"></u-album>
				</view>
			</view>
			<view style="height: 150rpx;width: 750rpx;"></view>
		</view>

		<view class="customerview">
			<view class="iconView">
				<u-icon class="iconView__icon" slot="inactive-icon" name="edit-pen" size="48rpx" color="#fff"
					:label="$t('astral.index.evaluation')" labelPos="bottom" labelSize="24rpx" space="2px"
					label-color="#fff"  @click="showWindow = true"></u-icon>
				<u-icon class="iconView__icon" slot="inactive-icon" :name="detailsInfo.is_liked?'heart-fill':'heart'"
					size="48rpx" :color="detailsInfo.is_liked?'#930301':'#fff'" :label="$t('astral.details.giveLike')"
					labelPos="bottom" labelSize="24rpx" space="2px" label-color="#fff" @click="dianzan"></u-icon>
				<u-icon class="iconView__icon" slot="inactive-icon"
					:name="detailsInfo.is_recommend?'thumb-up-fill':'thumb-up'" size="48rpx"
					:color="detailsInfo.is_recommend?'#930301':'#fff'" :label="$t('astral.details.recommended')"
					labelPos="bottom" labelSize="24rpx" space="2px" label-color="#fff" @click="tuijian"></u-icon>
			</view>
			<view class="lianxikefu" @click="showWindow = true">
				联系TA
			</view>
		</view>
		<view>
			<u-popup :show="showWindow" mode="center" bgColor="transparent">
				<view class="" style="position:relative;">
					<image src="@/static/nearby/beijing.png" style="width: 650rpx;height: 807rpx;"></image>
					<image src="@/static/upload/close.png" @click="showWindow = false" style="width: 70rpx;height: 70rpx;position: absolute;left:290rpx;bottom: -100rpx;"></image>
					<text style="position: absolute;top: 135rpx;left: 105rpx;font-size: 38rpx;font-weight: 500;">高质量社交平台</text>
					<view class="" style="position: absolute;top: 300rpx;left: 105rpx;right: 105rpx;bottom: 150rpx;font-size: 28rpx;color: #9c9d9d;overflow-y: auto;" v-html="detailsInfo.details"></view>
					<view style="position: absolute;bottom: 30rpx;background-image: linear-gradient(270deg, #FF7B3C 0%, #FF5B7C 100%);
border-radius: 43rpx;width: 440rpx;height: 86rpx;left: 105rpx;text-align: center;line-height: 86rpx;" @click="gokefu">联系客服</view>
				</view>
			</u-popup>
			<u-popup :show="showWx" mode="center" bgColor="transparent">
				<view class="" style="position:relative;">
					<image src="@/static/nearby/wxbeijing.png" style="width: 520rpx;height: 530rpx;"></image>
					<image src="@/static/upload/close.png" @click="showWx = false" style="width: 70rpx;height: 70rpx;position: absolute;left:225rpx;bottom: -100rpx;"></image>
					<text class="wxpopText" style="position: absolute;top: 160rpx;left: 105rpx;right: 70rpx;font-size: 38rpx;font-weight: 500;text-align: center;">你不是会员，暂时不能获取微信</text>
					<view style="position: absolute;bottom: 90rpx;background-image: linear-gradient(270deg, #E9C89A 0%, #FCE0C8 100%);
border-radius: 43rpx;width: 324rpx;height: 86rpx;left: 110rpx;text-align: center;line-height: 86rpx;color: #1F1F1F;" @click="gokefu">联系客服</view>
				</view>
			</u-popup>
			<u-popup :show="showID" mode="center" bgColor="transparent">
				<view class="" style="position:relative;">
					<image src="@/static/nearby/wxbeijing.png" style="width: 520rpx;height: 530rpx;"></image>
					<image src="@/static/upload/close.png" @click="showID = false" style="width: 70rpx;height: 70rpx;position: absolute;left:225rpx;bottom: -100rpx;"></image>
					<text class="wxpopText" style="position: absolute;top: 160rpx;left: 105rpx;right: 70rpx;font-size: 38rpx;font-weight: 500;text-align: center;">你不是会员，暂时不能复制ID</text>
					<view style="position: absolute;bottom: 90rpx;background-image: linear-gradient(270deg, #E9C89A 0%, #FCE0C8 100%);
border-radius: 43rpx;width: 324rpx;height: 86rpx;left: 110rpx;text-align: center;line-height: 86rpx;color: #1F1F1F;" @click="gokefu">联系客服</view>
				</view>
			</u-popup>
		</view>
	</view>
</template>
<script>
	import {
		mapState
	} from 'vuex'
	import mediaCache from '@/common/mediacache/mediaCache.js'; // 适配你的实际路径
	export default {
		name: 'astralDetailsNearby',
		data() {
			return {
				showWindow:false,
				showWx:false,
				showID:false,
				detailsInfo: {
					cover_url:''
				},
				id: '',
				innerAudioContext:null
			}
		},
		onLoad() {
			let {
				id
			} = this.$Route.query
			this.id = id
			this.getDetail()
			this.innerAudioContext = uni.createInnerAudioContext();
		},
		onUnload() {
			if(this.innerAudioContex){
				this.innerAudioContex.destroy()
				this.innerAudioContex = null
			}
		},
		computed: {
			...mapState({
				mykefu: state => state.friends.mykefu,
			}),
			getAlbuma() {
				return this.detailsInfo.stars_user_images && this.detailsInfo.stars_user_images.map(item => {
					return {
						src:item.image_url,
						videoSrc:item.image_url,
						image_type:item.image_type
					}
				}) || []
			}
		},
		onShow() {},
		methods: {
			maskString(str) {
				if(!str)return ''
			    if (str.length <= 2) {
			        return str; // 如果字符串长度小于等于2，直接返回
			    }
			    const visiblePart = str.slice(0, 2); // 保留前两位
			    const maskedPart = '*'.repeat(str.length - 2); // 用*替换其余部分
			    return visiblePart + maskedPart;
			},
			bofang(){
				if(this.innerAudioContext){
					if(this.detailsInfo.audio_info.play){
						this.innerAudioContext.stop()
						this.$set(this.detailsInfo.audio_info,'play',false)
						return
					}
					if(this.detailsInfo.audio_info.urlError){
						uni.showToast({
							title:'音频加载失败',
							icon:'none'
						})
						return
					}
					if(!this.detailsInfo.audio_info.url){
						uni.showToast({
							title:'音频加载中',
							icon:'none'
						})
						return
					}
					//播放中
					this.$set(this.detailsInfo.audio_info,'play',true)
					console.log(this.detailsInfo.audio_info);
					this.innerAudioContext.stop()
					this.innerAudioContext.src = this.detailsInfo.audio_info.url;
					this.innerAudioContext.play()
					this.innerAudioContext.onPlay(() => {
					  console.log('开始播放');
					});
					this.innerAudioContext.onError((res) => {
					  console.log(res.errMsg);
					  console.log(res.errCode);
					});
					this.innerAudioContext.onCanplay((res)=>{
						console.log(res);
					})
					this.innerAudioContext.onEnded((res)=>{
						this.$set(this.detailsInfo.audio_info,'play',false)
					})
				}
			},
			isNetworkUrl(url) {
			    const pattern = /^(http|https|ftp):\/\//i;
			    return pattern.test(url);
			},
			async getDetail() {
				try {
					let apires = await this.$api.common.showUserInfo({
						star_user_id: this.id
					})
					if (apires) {
						this.detailsInfo = apires.data
						if(this.detailsInfo.audio_info){
							this.$set(this.detailsInfo.audio_info,'play',false)
							mediaCache.loadMedia(this.detailsInfo.audio_info.thumb_url, 'stars', 'audio').then(resaudio=>{
								this.detailsInfo.audio_info.url = resaudio
							}).catch(e=>{
								this.$set(this.detailsInfo.audio_info,'urlError',true)
							});
						}
						for (let index in this.detailsInfo.stars_user_images) {
							let item = this.detailsInfo.stars_user_images[index]
							try {
								// this.detailsInfo.stars_user_images[index].image_url = '/static/logo.png'
								const cachedMedia = await mediaCache.loadMedia(item.image_url, 'stars', 'image');
								if(this.isNetworkUrl(cachedMedia) || item.image_type == '0'){
									this.detailsInfo.stars_user_images[index].image_url = cachedMedia
								}else{
									 // 使用plus.io的resolveLocalFileSystemURL方法获取文件的绝对路径
									 plus.io.resolveLocalFileSystemURL(cachedMedia, (entry) => {
										 this.detailsInfo.stars_user_images[index].image_url = entry.toLocalURL()
									 }, function(e) {});
								}
							} catch (error) {
								console.log(error);
								this.detailsInfo.stars_user_images[index].image_url = '/static/logo.png'
							}
						}
					}
				} catch (e) {
					//TODO handle the exception
				}
			},
			gokefu() {
				if (this.mykefu) {
					this.$Router.push({
						name: 'chatMsg',
						params: {
							friend_idx: this.mykefu.user_id,
							nick_name: this.mykefu.nick_name,
							avatar_url: this.mykefu.avatar_url,
						}
					});
				} else {
					uni.showToast({
						title: this.$t("astral.details.noContact"),
						icon: 'none'
					})
				}
			},
			dianzan() {
				if (this.detailsInfo.is_liked == '0') {
					this.$api.common.starsUserAdd({
						star_user_id: this.detailsInfo.id,
						function_type: 0
					}).then(res => {
						if (res) {
							this.detailsInfo.is_liked = res.data.id
						}
					})
				} else {
					this.$api.common.starsUserDel({
						star_user_id: this.detailsInfo.id,
						operate_id: this.detailsInfo.is_liked
					}).then(res => {
						if (res) {
							this.detailsInfo.is_liked = 0
						}
					})
				}
			
			},
			tuijian() {
				if (this.detailsInfo.is_recommend == '0') {
					this.$api.common.starsUserAdd({
						star_user_id: this.detailsInfo.id,
						function_type: 1
					}).then(res => {
						if (res) {
							this.detailsInfo.is_recommend = res.data.id
						}
					})
				} else {
					this.$api.common.starsUserDel({
						star_user_id: this.detailsInfo.id,
						operate_id: this.detailsInfo.is_recommend
					}).then(res => {
						if (res) {
							this.detailsInfo.is_recommend = 0
						}
					})
				}
			},
		},
	};
</script>

<style scoped lang="scss">
	image {
		display: block;
	}
	.wxpopText{
		color: #bca376;
		font-size: 40rpx;
	}
	.topImageView {
		image {
			width: 750rpx;
			height: 580rpx;
		}
	}

	.contentView {
		position: relative;
		background-image: linear-gradient(180deg, #242F64 0%, #242E63 34%, #040210 100%);
		flex: 1;
		padding: 30rpx;
		z-index: 10;

		&__infoview {
			display: flex;
			align-items: center;
			margin-top: -70rpx;

			&__nameview {
				z-index: 10;
				margin-left: 20rpx;

				&__idview {
					display: flex;
					align-content: center;
					margin-top: 10rpx;
				}
			}
		}

		&__tagsview {
			margin-top: 20rpx;
			display: flex;
			flex-wrap: wrap;
			gap: 20rpx;

			&__item {
				background: #2f386a;
				border-radius: 6px;
				font-size: 28rpx;
				color: #c0c3d2;
				padding: 5rpx 10rpx;
			}
		}

		&__textview {
			margin-top: 30rpx;
			font-size: 28rpx;
			color: #8e94af;
			font-weight: 400;
		}

		&__wchatview {
			background: rgba(255, 255, 255, 0.05);
			border-radius: 12rpx;
			padding: 30rpx 20rpx;
			margin-top: 50rpx;

			&__title {
				font-size: 32rpx;
				color: #FFFFFF;
				font-weight: 500;
			}

			&__infoview {
				margin-top: 10rpx;
				display: flex;
				justify-content: space-between;
				align-items: flex-end;

				&__infodel {
					display: flex;
					align-items: center;

					image {
						width: 40rpx;
						height: 40rpx;
					}

					text {
						color: #8e94af;
						font-size: 32rpx;
						margin-left: 10rpx;
					}
				}

				&__btn {
					background: rgba(255, 255, 255, 0.10);
					box-shadow: 0px 2px 0px 0px rgba(0, 0, 0, 0.2);
					border-radius: 6px;
					padding: 10rpx 20rpx;
					font-size: 28rpx;
				}
			}
		}

		&__photoAlbum {
			background: rgba(0, 0, 0, 0.05);
			border-radius: 12rpx;
			padding: 30rpx 20rpx;
			margin-top: 30rpx;

			&__title {
				font-size: 32rpx;
				color: #FFFFFF;
				font-weight: 500;
			}

			&__phoneView {
				margin-top: 30rpx;
			}
		}

		&__voice {
			display: flex;
			align-content: center;
			position: absolute;
			right: 20rpx;
			top: -95rpx;

			image {
				width: 150rpx;
				height: 48rpx;
			}

			text {
				font-size: 30rpx;
				color: #FF8686;
				margin-left: 10rpx;
			}
		}
	}

	.customerview {
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 999;
		width: 750rpx;
		height: 150rpx;
		text-align: center;
		background: #0C081F;
		display: flex;
		align-items: center;
		padding: 0 50rpx 0 0;
		box-sizing: border-box;

		.iconView {
			display: flex;
			align-items: center;
			flex: 1;
			justify-content: space-around;
			padding: 0 30rpx;

			&__icon {}
		}

		.lianxikefu {
			background: linear-gradient(270deg, #FF7B3C 0%, #FF5B7C 100%);
			border-radius: 44rpx;
			height: 88rpx;
			display: flex;
			align-items: center;
			padding: 0 80rpx;
			font-size: 32rpx;
			color: #FFFFFF;
		}
	}
</style>